package com.raj.sample;

import java.util.HashMap;

import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;

public class SNMPReceiver implements ResponseListener {
	private String key;
	SNMPSender sender;

	public SNMPReceiver(SNMPSender commonSender) {
		sender = commonSender;
	}

	public void onResponse(ResponseEvent event) {
		((Snmp) event.getSource()).cancel(event.getRequest(), this);
		PDU response = event.getResponse();
		System.out.println(event.getUserObject() + " : " + response);
		HashMap<String, String> mibRes = new HashMap<String, String>();
		if (null != response) {
			for (Object item : response.getVariableBindings()) {
				if (null != item && item.toString().indexOf("=") != -1) {
					int index = item.toString().indexOf("=");
					mibRes.put(item.toString().substring(0, index), item.toString().substring(index + 1));
				}
			}
			sender.callback(key, mibRes);
		}
	}

	public void setKey(String key) {
		this.key = key;
	}

}